/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * SpelerToevoegen.java
 *
 * Created on 13-dec-2011, 11:26:36
 */
package view;
import model.Speler;
import javax.swing.*;
/**
 *
 * @author tomkeim
 */
public class SpelerWijzigen extends javax.swing.JFrame {
    private Hoofdscherm hscherm;
    /** Creates new form SpelerToevoegen */
    Speler eenSpeler = new Speler();
    
    public SpelerWijzigen() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        naamField = new javax.swing.JTextField();
        adresField = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        postcodeField = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        woonplaatsField = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        telefoonField = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        emailField = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        ratingField = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        spelerWijzigButton = new javax.swing.JButton();
        searchButton = new javax.swing.JButton();
        searchField = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Speler Wijzigen");
        setLocationByPlatform(true);

        jLabel1.setText("Naam");

        naamField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                naamFieldActionPerformed(evt);
            }
        });

        jLabel2.setText("Adres");

        jLabel3.setText("Postcode");

        jLabel4.setText("Woonplaats");

        jLabel5.setText("Telefoon");

        jLabel6.setText("Email");

        jLabel7.setText("Rating");

        spelerWijzigButton.setText("Speler Wijzigen");
        spelerWijzigButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                spelerWijzigButtonActionPerformed(evt);
            }
        });

        searchButton.setText("Zoeken");
        searchButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                searchButtonActionPerformed(evt);
            }
        });

        searchField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                searchFieldActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(spelerWijzigButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 469, Short.MAX_VALUE)
                            .add(layout.createSequentialGroup()
                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                    .add(jLabel3)
                                    .add(jLabel2)
                                    .add(jLabel1)
                                    .add(jLabel4)
                                    .add(jLabel5)
                                    .add(jLabel6)
                                    .add(jLabel7))
                                .add(18, 18, 18)
                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                    .add(emailField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 394, Short.MAX_VALUE)
                                    .add(telefoonField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 394, Short.MAX_VALUE)
                                    .add(woonplaatsField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 394, Short.MAX_VALUE)
                                    .add(naamField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 394, Short.MAX_VALUE)
                                    .add(adresField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 394, Short.MAX_VALUE)
                                    .add(postcodeField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 394, Short.MAX_VALUE)
                                    .add(org.jdesktop.layout.GroupLayout.TRAILING, ratingField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 394, Short.MAX_VALUE))))
                        .addContainerGap())
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                        .add(searchField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 75, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(26, 26, 26)
                        .add(searchButton)
                        .add(20, 20, 20))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(searchButton)
                    .add(searchField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(27, 27, 27)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel1)
                    .add(naamField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel2)
                    .add(adresField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel3)
                    .add(postcodeField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel4)
                    .add(woonplaatsField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel5)
                    .add(telefoonField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel6)
                    .add(emailField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(8, 8, 8)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel7)
                    .add(ratingField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 15, Short.MAX_VALUE)
                .add(spelerWijzigButton))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void searchButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_searchButtonActionPerformed
    try {
    String id = searchField.getText();
    int spelerId = Integer.parseInt(id);
    if (Speler.exists(spelerId)) {
    eenSpeler = Speler.getSpeler(spelerId);
    
    String naam = eenSpeler.getNaam();
    naamField.setText(naam);
    String adres = eenSpeler.getAdres();
    adresField.setText(adres);
    String postcode = eenSpeler.getPostcode();
    postcodeField.setText(postcode);
    String woonplaats = eenSpeler.getWoonplaats();
    woonplaatsField.setText(woonplaats);
    String telefoon = eenSpeler.getTelefoonNummer();    
    telefoonField.setText(telefoon);
    String email = eenSpeler.getEmail();
    emailField.setText(email);
    double rating = eenSpeler.getRating();
    String ratingString = String.valueOf(rating);
    ratingField.setText(ratingString);
    }
    else {
        JOptionPane.showMessageDialog(null, "De speler met het id " + spelerId + " is niet gevonden.");
    }
     
             
    } 
    catch (NumberFormatException e) {
        JOptionPane.showMessageDialog(null, "Voer een Speler ID in");
    }
    // TODO add your handling code here:
}//GEN-LAST:event_searchButtonActionPerformed

private void searchFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_searchFieldActionPerformed
    
    // TODO add your handling code here:
}//GEN-LAST:event_searchFieldActionPerformed

private void spelerWijzigButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_spelerWijzigButtonActionPerformed
try {
    int confirm = JOptionPane.showConfirmDialog(null, "Weet u zeker dat u deze gegevens wilt wijzigen?");
if (confirm == JOptionPane.YES_OPTION) {
    

   String naami = naamField.getText();
    eenSpeler.setNaam(naami);
    String adresi = adresField.getText();
    eenSpeler.setAdres(adresi);
    String postcodei = postcodeField.getText();
    eenSpeler.setPostcode(postcodei);
    String emaili = emailField.getText();
    eenSpeler.setEmail(emaili);
    String woonplaatsi = woonplaatsField.getText();
    eenSpeler.setWoonplaats(woonplaatsi);
    String telefoonnri = telefoonField.getText();
    eenSpeler.setTelefoonNummer(telefoonnri);
    String ratingFieldString = ratingField.getText();
    double ratingDouble = Double.parseDouble(ratingFieldString);
    eenSpeler.setRating(ratingDouble);

    String empty = "";
    if (!naami.equals(empty) && !adresi.equals(empty) && !postcodei.equals(empty) && !emaili.equals(empty) && !woonplaatsi.equals(empty) && !woonplaatsi.equals(empty) && !ratingFieldString.equals(empty)) {
    if (eenSpeler.update() == true) {
        JOptionPane.showMessageDialog(null, "De gegevens zijn succesvol gewijzigd");
    }
    else 
    {
        JOptionPane.showMessageDialog(null, "De gegevens zijn niet gewijzigd.");
    }
    
   
}
    else
    {
        JOptionPane.showMessageDialog(null, "U mag geen velden leeglaten.");
    }
    }
else
{
     JOptionPane.showMessageDialog(null, "De gegevens zijn niet gewijzigd.");
} 
}
catch (NumberFormatException e) {
    JOptionPane.showMessageDialog(null, "Voer alle gegevens goed in.");
}
    // TODO add your handling code here:
}//GEN-LAST:event_spelerWijzigButtonActionPerformed

private void naamFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_naamFieldActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_naamFieldActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(SpelerWijzigen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(SpelerWijzigen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(SpelerWijzigen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(SpelerWijzigen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new SpelerWijzigen().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField adresField;
    private javax.swing.JTextField emailField;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JTextField naamField;
    private javax.swing.JTextField postcodeField;
    private javax.swing.JTextField ratingField;
    private javax.swing.JButton searchButton;
    private javax.swing.JTextField searchField;
    private javax.swing.JButton spelerWijzigButton;
    private javax.swing.JTextField telefoonField;
    private javax.swing.JTextField woonplaatsField;
    // End of variables declaration//GEN-END:variables
}
